<template>
  <div>
    <el-card class="box-card">
      <div :gutter="20" class="father">
        <div class="left">
          <div class="grid-content bg-purple">
            <span class="subject">标签名称</span>
            <el-input size="small" v-model="params.tagName"> </el-input>
            <span class="subject">状态</span>
            <el-select v-model="params.state" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-button size="mini" @click="clearSearch">清除</el-button>
            <el-button size="mini" type="primary" @click="searchSubject"
              >搜索</el-button
            >
          </div>
        </div>
        <div class="right"
          ><div class="grid-content bg-purple">
            <el-button v-if="this.historyRouter" @click="getback" size="mini"
              ><i class="el-icon-back"></i> 返回学科</el-button
            >
            <el-button @click="addSubject" size="mini" type="success"
              ><i class="el-icon-edit"></i> 新增标签</el-button
            >
          </div></div
        >
      </div>
      <p class="count">
        <i class="el-icon-info"></i>
        {{ cTitle }}
      </p>
      <el-table :data="list" style="width: 100%">
        <el-table-column type="index" :index="evenNumber" label="序号">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="tagName" label="标签名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template #default="{ row }">
            {{ adddate(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template #default="{ row }">
            {{ isDisplay(row.state) }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template #default="{row}">
            <el-button @click="checkDual(row)" type="text" size="small">{{row.state?'禁用':'启用'}}</el-button>
            <el-button :disabled="row.state?false:true" @click="editDirectory(row)" type="text" size="small">修改</el-button>
            <el-button :disabled="row.state?false:true" @click="delDirectory(row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          background
          @current-change="changePage"
          @size-change="changePagesize"
          :page-size="params.pagesize"
          :current-page="params.page"
          :page-sizes="[10, 20, 50, 100]"
          layout="prev, pager,sizes, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <tags-add ref="formDialog" @closeDialog="closeDialog" :addDialogVisible="addDialogVisible" />
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/tags.js'
import * as dayjs from 'dayjs'
import TagsAdd from '../components/tags-add.vue'
export default {
  name: 'Tags',
  // name: 'Directorys',
  components: { TagsAdd },
  data () {
    return {
      list: [],
      total: 100000000,
      params: {
        page: 1,
        pagesize: 10
      },
      search: '',
      historyRouter: '',
      addDialogVisible: false,
      options: [{
        value: 0,
        label: '禁用'
      }, {
        value: 1,
        label: '启用'
      }]

    }
  },
  mounted () {
    this.getList()
  },
  computed: {
    cTitle () {
      return `数据一共${this.total}条`
    }
  },
  methods: {
    async getList () {
      this.historyRouter = this.$route.query.row
      if (this.historyRouter) {
        this.params = {
          page: 1,
          pagesize: 10,
          subjectID: this.historyRouter.id
        }
      }
      const res = await list(this.params)
      this.list = res.data.items
      this.total = res.data.counts
    },
    evenNumber (index) {
      return index + (this.params.page - 1) * this.params.pagesize + 1
    },
    changePage (page) {
      this.params.page = page
      this.getList()
    },
    changePagesize (pagesize) {
      this.params.pagesize = pagesize
      this.params.page = 1
      this.getList()
    },
    async searchSubject () {
      this.params.page = 1
      const res = await list(this.params)
      this.list = res.data.items
      this.total = res.data.counts
    },
    clearSearch () {
      this.params = {
        page: 1,
        pagesize: 10
      }
      this.params.page = 1
      this.getList()
    },
    adddate (value) {
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    },
    isDisplay (value) {
      const map = {
        0: '否',
        1: '是'
      }
      return map[value]
    },
    isFlag (value) {
      const map = {
        false: 0,
        true: 1
      }
      return map[value]
    },
    addSubject () {
      this.addDialogVisible = true
    },
    closeDialog () {
      this.addDialogVisible = false
      this.getList()
    },
    // 编辑
    editDirectory (id) {
      console.log(id)
      this.addDialogVisible = true
      this.$refs.formDialog.editDirectory(id)
    },
    // 删除
    delDirectory (id) {
      this.$confirm('确定要删除该学科吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(id)
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async checkDual (row) {
      const newState = this.isFlag(!row.state)
      await changeState({
        id: row.id,
        state: newState
      })
      console.log(newState)
      this.$message.success('操作成功')
      this.getList()
    },
    getback () {
      this.$router.go(-1)
    }

  }
}
</script>

<style scoped lang='css'>
.page {
  position: relative;
  height: 50px;
}
.el-pagination {
  position: absolute;
  right: 0;
  bottom: 0;
  margin-top: -20px;
}
.grid-content {
  display: flex;
  margin-bottom: 10px;
}
.el-input,.el-select {
  margin-right: 20px;
  margin-left: 20px;
  width: 200px;
}
.subject {
  margin-top: 5px;
}
.count {
  color: #909399;
  background-color: #f4f4f5;
  padding: 10px;
}
.el-row {
  padding-right: 10px;
}
.left{
  float: left;
}
.right{
  float: right;
}
.father{
  width: 100%;
  height: 50px;
}
</style>
